<?php 
error_reporting(E_ERROR);
session_start();
require_once('constants_shop.php');
require_once('functions.php');
include('classes/userClass.php');



#conect to shop database	
$shopObj = new mysqlClass();

//update for upload carousel
if(isset($_POST['upload_carousel'])){
	$path_parts = pathinfo($_FILES['Filedata']['name']);
	
	$time = time();
	$image_path = $path_parts['filename'].'_'.$time.'.'.$path_parts['extension'];
	// $thumb_path = $path_parts['filename'] .$time. '_thumb.' . $path_parts['extension'];

	//path inserted in database
	$insert_path = 'carousel/'.$image_path;
	$targetFolderImg = "../../shop/images/carousel/";
	// $targetFolderthumb = "../../img/album_theme_" . $_POST['album_theme_id'] . "/";
	if ( !is_dir($targetFolderImg) ) {
		mkdir($targetFolderImg);
	}
	
	move_uploaded_file($_FILES["Filedata"]["tmp_name"], $targetFolderImg.$image_path);
	$params=array();

	// insert into carousel table
	$params['page_id']=$_POST['page_id'];
	$params['picture'] = $insert_path;
	$last_id=$shopObj->insert('carousel',$params);

	
	$available_lang= $shopObj->fetch_result("SELECT * FROM languages WHERE active=1");
	echo '<div style="margin-bottom:5px;margin-left:10px">
	<div class="span2" style="text-align:center;;margin-left:10px">';
	echo "<div onclick='get_carousel_info(".$last_id.",this);' style='margin-left:auto;margin-right:auto;cursor:pointer;width:109px;height:100px;background:url(".SERVER_PATH."../shop/images/{$insert_path}) center center no-repeat;background-size:150px auto' ></div>";	
	
	echo '<br class="clear"/>
	<button class="btn btn-mini btn-danger" style="margin-top:5px;margin-bottom:5px" onclick="delete_carousel_img('.$last_id.',this);"><i class="icon-trash "></i>Delete</button>
	</div></div>';

	foreach($available_lang as $value){
		$params = array();
		$params['carousel_id']=$last_id;
		$params['language_id']=$value['id'];
		$params['description']='';
		$params['title']='';
		$params['link']='';
		$insert_carousel_details = $shopObj->insert('carousel_details',$params);	
	}

	// print json_encode($json);
}

if(isset($_POST['upload_pictures'])){
	$path_parts = pathinfo($_FILES['Filedata']['name']);
	
	$time = time();
	$image_path = $path_parts['filename'].'_'.$time.'.'.$path_parts['extension'];
	// $thumb_path = $path_parts['filename'] .$time. '_thumb.' . $path_parts['extension'];

	//path inserted in database
	$insert_path = 'images/site_pages/'.$image_path;
	$targetFolderImg = "../../shop/images/site_pages/";
	// $targetFolderthumb = "../../img/album_theme_" . $_POST['album_theme_id'] . "/";
	
	if ( !is_dir($targetFolderImg) ) {
		mkdir($targetFolderImg);
	}

	list($originalWidth, $originalHeight) = getimagesize($_FILES['Filedata']['tmp_name']);
	$thumbWidth = 800;
	$raportThumb = $originalWidth / $thumbWidth;
	$thumbHeight = $originalHeight / $raportThumb;
	$thumbCanvas = imagecreatetruecolor($thumbWidth, $thumbHeight);
	$originalImage = imagecreatefromjpeg($_FILES['Filedata']['tmp_name']);
	imagecopyresized($thumbCanvas, $originalImage, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $originalWidth, $originalHeight);
	imagejpeg($thumbCanvas, $targetFolderImg.$image_path, 60);
	imagedestroy($originalImage);


	$params=array();

	// insert into carousel table
	$params=array();
	$params['title']='';
	$params['name'] = $insert_path;
	$params['thumb_name'] = '';
	$last_id=$shopObj->insert('picture_files',$params);

	$params=array('site_page_id'=>$_POST['page_id'],'picture_id'=>$last_id);
	$last_id=$shopObj->insert('site_page_pictures',$params);
	echo "<div style='width:150px; margin-left:5px;text-align:center;float:left'>
	<div style='width:150px;height:150px;background:url(".SERVER_PATH."../shop/{$insert_path}) center center no-repeat;background-size:150px auto'></div>
	
	<br class='clear'/>
	<button class='btn btn-mini btn-danger' style='margin-top:5px;margin-bottom:5px' onclick='delete_page_img(".$last_id.",this);'><i class='icon-trash '></i>Delete</button>
	</div>";
}

if(isset($_POST['upload_csv'])){
		

	$handle = fopen($_FILES['filename']['tmp_name'], "r");
	 
	    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	      		 $shopObj->execute_query("UPDATE stocks SET quantity='{$data[1]}',price='{$data[2]}' WHERE barcode='{$data[0]}'"); 
	       
	    }
}
 	

?>